Imports MIMS_WRS.Business.Common

Public Class LookupDataList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents btnRemove As System.Web.UI.WebControls.Button
    Protected WithEvents dgLKDList As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblPageTitle As System.Web.UI.WebControls.Label
    Protected WithEvents btnAdd As System.Web.UI.WebControls.Button
    Protected WithEvents btnBack As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected ReadOnly Property AddLookupDataPopupURL() As String
        Get
            Return Server.UrlEncode(AppRoot() & "/Dialogs/LookupDataInputDialog.aspx?id=" & Me.LookupID.ToString)
        End Get
    End Property

    Protected ReadOnly Property LookupID() As Integer
        Get
            Try
                Return CInt(Request("id"))
            Catch
                Throw New ApplicationException("Invalid or missing look up ID")
            End Try
        End Get
    End Property

    Private Sub BindGrid()
        Me.dgLKDList.DataSource = CLookup.GetLookupDataList(LookupID)
        Me.dgLKDList.DataBind()
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then

            Dim obj As New CLookup
            obj.Load(LookupID)

            Me.lblPageTitle.Text = "Lookup Data for " & obj.CategoryName

            ' column 0 is for check box
            Me.dgLKDList.Columns(1).HeaderText = obj.Title1
            Me.dgLKDList.Columns(2).HeaderText = obj.Title2
            Me.dgLKDList.Columns(3).HeaderText = obj.Title3

            dgLKDList.Columns(1).Visible = Not NullOrEmpty(obj.Title1)
            dgLKDList.Columns(2).Visible = Not NullOrEmpty(obj.Title2)
            dgLKDList.Columns(3).Visible = Not NullOrEmpty(obj.Title3)

            Me.BindGrid()
        End If
    End Sub


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Me.BindGrid()
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        Dim dgi As DataGridItem
        For Each dgi In Me.dgLKDList.Items
            Dim chk As HtmlInputCheckBox
            chk = CType(dgi.Cells(0).FindControl("chkSelection"), HtmlInputCheckBox)
            If chk.Checked Then
                Try
                    Dim obj As New CLookupData
                    obj.Load(chk.Value)
                    obj.Remove()
                    obj.Save()
                    obj = Nothing
                Catch ex As Exception
                    Throw New ApplicationException("Failed to delete item " & chk.Value, ex)
                End Try
            End If
        Next
        Me.BindGrid()
    End Sub

    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        Response.Redirect(AppRoot() & "/Admin/LookupList.aspx")
    End Sub
End Class
